package top.mataiji.manager.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.mataiji.manager.mapper.BookMapper;
import top.mataiji.manager.mapper.CartMapper;
import top.mataiji.manager.pojo.Book;
import top.mataiji.manager.pojo.BookExample;
import top.mataiji.manager.pojo.Cart;
import top.mataiji.manager.pojo.CartExample;
import top.mataiji.manager.pojo.vo.CartVo;
import top.mataiji.manager.service.CartService;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

/**
 * @Auther: zhaoss
 * @Date: 2021/3/21 - 03 - 21 - 10:51
 * @Description: top.mataiji.manager.service.impl
 * @version: 1.0
 */
@Service
public class CartServcieImpl implements CartService {

    @Autowired
    CartMapper cartMapper;

    @Autowired
    BookMapper bookMapper;

    /**
     * 添加购物车
     * @param cart 复合类，包含user，book
     * @return
     */
    @Override
    public int addBook(Cart cart) {
        return cartMapper.insertSelective(cart);
    }

    /**
     *
     * @param userId
     * @param bookId
     * @return 返回值为购物车表的id值，如果值不为0则在原来之上加上相应的值
     */
    @Override
    public Cart selectById(Integer userId, Integer bookId) {
        //创建查询对象
        CartExample example = new CartExample();
        //设置查询参数
        example.createCriteria().andBookIdEqualTo(bookId).andUserIdEqualTo(userId);
        //查询
        List<Cart> carts = cartMapper.selectByExample(example);
        if(!carts.isEmpty()&&carts!=null){
            return carts.get(0);
        }
        return null;
    }

    /**
     * 更新购物车信息
     * @param i
     * @return
     */
    @Override
    public int updateBook(Cart i) {
        return cartMapper.updateByPrimaryKeySelective(i);
    }

    @Override
    public List<CartVo> findAllUserId(Integer userId) {
        //查询购物车
        //创建书籍对象
        CartVo cartVo = null;
        List<CartVo> cartVos = new ArrayList<>();
        //创建查询对象
        CartExample example = new CartExample();
        //设置查询参数
        example.createCriteria().andUserIdEqualTo(userId);
        //查询,得到数据查询书籍
        List<Cart> carts = cartMapper.selectByExample(example);
        ListIterator<Cart> cartListIterator = carts.listIterator();
        while (cartListIterator.hasNext()){
//            cartDtd = new CartDtd();
            cartVo = new CartVo();
            Cart cart = cartListIterator.next();
            cartVo.setCart(cart);
//            cartDtd.setUserId(userId);
//            cartDtd.setBookNum(cart.getBookNum());
            //创建查询对象
            BookExample bookExample = new BookExample();
            //设置查询参数
            bookExample.createCriteria().andBookidEqualTo(cart.getBookId());
            //查询书籍
            List<Book> books1 = bookMapper.selectByExample(bookExample);
            if(books1!=null&&!books1.isEmpty()){
                cartVo.setBook(books1.get(0));
//                Book book = books1.get(0);
//                cartDtd.setBookNmae(book.getBookname());
//                cartDtd.setBookPrice(book.getBookprice());
            }
            cartVos.add(cartVo);
//            cartDtds.add(cartDtd);
        }
        return cartVos;
    }

    @Override
    public Integer deleteCartById(Integer teid) {
        //
        return cartMapper.deleteByPrimaryKey(teid);
    }

    @Override
    public Integer change(Integer teid,Integer num) {
        Cart cart = cartMapper.selectByPrimaryKey(teid);
        cart.setBookNum(cart.getBookNum()+num);
        return cartMapper.updateByPrimaryKeySelective(cart);
    }
}
